import { routerRedux } from 'dva/router';
import { accountLogin, getRole  } from '../services/api';
import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';
import G from '../utils/g';

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
    *submit({ payload }, { call, put }) {
      //const response = yield call(fakeAccountLogin, payload);
     /* yield put({
        type: 'changeLoginStatus',
        payload: true,
      });*/
      // yield put({
      //   type: 'changeLoginStatus',
      //   payload: {
      //     status: true,
      //     currentAuthority: 'normal',
      //   },
      // });
      const response = yield call(accountLogin, payload);

      if(response){  //成功返回的response为true;

        window.localStorage.token = response.accessToken;
        window.localStorage.userInfo = JSON.stringify(response.principal);
        G.setCookie('accessToken', response.accessToken);  // 上传文件需要使用
        // G.setCookie('X-APIHitgou-AccessToken', response.accessToken);  // 创建商家调用原有JAVA接口，需要从COOKIE中取token;

        const roleResponse = yield call(getRole,payload);
        console.log(roleResponse);
        if(roleResponse){
          yield put({
            type: 'changeLoginStatus',
            payload: {
              status: true,
              currentAuthority: roleResponse,
            },
          });
          reloadAuthorized();
          yield put(routerRedux.push('/'));
        }
      }else{
        yield put({
          type: 'changeSubmitting',
          payload: false,
        });
      }
      // Login successfully
      // if (response.status === 'ok') {
      //   reloadAuthorized();
      //   yield put(routerRedux.push('/'));
      // }

    },
    *logout(_, { put, select }) {
      try {
        // get location pathname
        const urlParams = new URL(window.location.href);
        let pathname = yield select(state => state.routing.location.pathname);
        if(pathname == '/admin/login'){
          pathname = '/dashboard/analysis'
        }
        // add the parameters in the url
        urlParams.searchParams.set('redirect', pathname);
        window.history.replaceState(null, 'login', urlParams.href);

        window.localStorage.token = '';  //清除本地缓存登录用户数据
        window.localStorage.userInfo = '';
        G.delCookie('accessToken');

      } finally {
        yield put({
          type: 'changeLoginStatus',
          payload: {
            status: false,
            currentAuthority: 'normal',
          },
        });
        reloadAuthorized();
        yield put(routerRedux.push('/admin/login'));
      }
    },
  },

  reducers: {
    save(state, {payload}){
      return {
        ...state,
        ...payload
      }
    },
    changeLoginStatus(state, { payload }) {
      setAuthority(payload.currentAuthority);
      //setAuthority('admin');
      return {
        ...state,
        status: payload.status,
        type: payload.type,
      };
    },
  },
};
